package com.example.demo.alipay.config;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 两个支付宝客户端，用户可以使用任意一个.
 *
 * alipay-trade-sdk 是对alipay-sdk-java的封装 建议使用alipay-trade-sdk
 *
 */
@Data
@Slf4j
@ConfigurationProperties(prefix = "pay.alipay")
public class AlipayProperties {

	/** 支付宝gatewayUrl */
	@Value("${alipay.gatewayUrl}")
	private String gatewayUrl;
	/** 商户应用id */
	@Value("${alipay.appid}")
	private String appid;
	/** RSA私钥，用于对商户请求报文加签 */
	@Value("${alipay.appPrivateKey}")
	private String appPrivateKey;
	/** 支付宝RSA公钥，用于验签支付宝应答 */
	@Value("${alipay.alipayPublicKey}")
	private String alipayPublicKey;
	/** 签名类型 */
	private String signType = "RSA2";
	/** 格式 */
	private String formate = "json";
	/** 编码 */
	private String charset = "UTF-8";
	/** 同步地址 */
	@Value("${alipay.returnUrl}")
	private String returnUrl;

	/** 异步地址 */
	@Value("${alipay.notifyUrl}")
	private String notifyUrl;

	/** 最大查询次数 */
	private static int maxQueryRetry = 5;

	/** 查询间隔（毫秒） */
	private static long queryDuration = 5000;
	/** 最大撤销次数 */
	private static int maxCancelRetry = 3;
	/** 撤销间隔（毫秒） */
	private static long cancelDuration = 3000;

	public String getGatewayUrl() {
		return gatewayUrl;
	}

	public void setGatewayUrl(String gatewayUrl) {
		this.gatewayUrl = gatewayUrl;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getAppPrivateKey() {
		return appPrivateKey;
	}

	public void setAppPrivateKey(String appPrivateKey) {
		this.appPrivateKey = appPrivateKey;
	}

	public String getAlipayPublicKey() {
		return alipayPublicKey;
	}

	public void setAlipayPublicKey(String alipayPublicKey) {
		this.alipayPublicKey = alipayPublicKey;
	}

	public String getSignType() {
		return signType;
	}

	public void setSignType(String signType) {
		this.signType = signType;
	}

	public String getFormate() {
		return formate;
	}

	public void setFormate(String formate) {
		this.formate = formate;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public static int getMaxQueryRetry() {
		return maxQueryRetry;
	}

	public static void setMaxQueryRetry(int maxQueryRetry) {
		AlipayProperties.maxQueryRetry = maxQueryRetry;
	}

	public static long getQueryDuration() {
		return queryDuration;
	}

	public static void setQueryDuration(long queryDuration) {
		AlipayProperties.queryDuration = queryDuration;
	}

	public static int getMaxCancelRetry() {
		return maxCancelRetry;
	}

	public static void setMaxCancelRetry(int maxCancelRetry) {
		AlipayProperties.maxCancelRetry = maxCancelRetry;
	}

	public static long getCancelDuration() {
		return cancelDuration;
	}

	public static void setCancelDuration(long cancelDuration) {
		AlipayProperties.cancelDuration = cancelDuration;
	}

	private AlipayProperties() {
	}

	@PostConstruct
	public void init() {
		System.out.println(description());
	}

	public String description() {
		StringBuilder sb = new StringBuilder("\nConfigs{");
		sb.append("支付宝网关: ").append(gatewayUrl).append("\n");
		sb.append(", appid: ").append(appid).append("\n");
		sb.append(", 商户RSA私钥: ").append(getKeyDescription(appPrivateKey)).append("\n");
		sb.append(", 支付宝RSA公钥: ").append(getKeyDescription(alipayPublicKey)).append("\n");
		sb.append(", 签名类型: ").append(signType).append("\n");

		sb.append(", 查询重试次数: ").append(maxQueryRetry).append("\n");
		sb.append(", 查询间隔(毫秒): ").append(queryDuration).append("\n");
		sb.append(", 撤销尝试次数: ").append(maxCancelRetry).append("\n");
		sb.append(", 撤销重试间隔(毫秒): ").append(cancelDuration).append("\n");
		sb.append("}");
		return sb.toString();
	}

	private String getKeyDescription(String key) {
		int showLength = 6;
		if (StringUtils.isNotEmpty(key) && key.length() > showLength) {
			return new StringBuilder(key.substring(0, showLength)).append("******")
					.append(key.substring(key.length() - showLength)).toString();
		}
		return null;
	}

}
